/**
 * Created by Feil.Wang(wangfeilong@xuexibao.cn) on 2015/12/24.
 */
var photos = require('./photos');
var user = require('./user');
var multer = require('multer');

function checkLogin(req, res, next) {
    if (!req.session.userId) {
        return res.redirect('/login');
    }
    next();
}

function routes(app) {
    var upload = multer({dest: app.get('photos')});
    app.use(upload.single('image'));
    app.get('/', checkLogin, photos.list);
    app.get('/upload', checkLogin, photos.form);
    app.post('/upload', checkLogin, photos.submit());

    app.get('/register', user.registerView);
    app.post('/register', user.register);

    app.get('/login', user.loginView);
    app.post('/login', user.login);

    app.get('/logout', user.logout);
}

module.exports = routes;
